﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Application.SystemManage;
using ZFine.Code;
using System.Web.Mvc;
using Entity.ProjectEnt;
using ZFine.Application.Project;
using ZFine.Domain.Entity;


namespace ZFine.Web.Areas.SystemManage.Controllers
{
    public class UserController : ControllerBase
    {
        private UserApp userApp = new UserApp();
        private UserLogOnApp userLogOnApp = new UserLogOnApp();
        private PositionRecordApp positoonapp = new PositionRecordApp();

        
        public ActionResult Updateposition(string TougouyuanUser,string DispatcherUser, string DiaochezhangUser, string ZhongzhuanyuanUser, string SijiUser, string FusijiUser, string HuoyunyunaUser1, string HuoyunyunaUser2, string daokouyuanUser1, string daokouyuanUser2, string BanCiUser)
        {
            int returncode = 0;
            string resultmsg = string.Empty;
            PositionRecordEntity newpositionrecord = new PositionRecordEntity();
            //if (string.IsNullOrEmpty(ProductionUser))
            //{
            //    returncode = -1;
            //}
            //else
            //{
            //    newpositionrecord.ProductionUser=new UserEntity{F_Id = ProductionUser.Trim() };
            //}
            if (string.IsNullOrEmpty(DispatcherUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.DispatcherUser = new UserEntity { F_Id = DispatcherUser.Trim() };
            }
            if (string.IsNullOrEmpty(TougouyuanUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.TougouyuanUser = new UserEntity { F_Id = TougouyuanUser.Trim() };
            }
            if (string.IsNullOrEmpty(DiaochezhangUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.DiaochezhangUser = new UserEntity { F_Id = DiaochezhangUser.Trim() };
            }
            if (string.IsNullOrEmpty(ZhongzhuanyuanUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.ZhongzhuanyuanUser = new UserEntity { F_Id = ZhongzhuanyuanUser.Trim() };
            }
            if (string.IsNullOrEmpty(SijiUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.SijiUser = new UserEntity { F_Id = SijiUser.Trim() };
            }
            if (string.IsNullOrEmpty(FusijiUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.FusijiUser = new UserEntity { F_Id = FusijiUser.Trim() };
            }
            if (string.IsNullOrEmpty(HuoyunyunaUser1))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.HuoyunyunaUser1 = new UserEntity { F_Id = HuoyunyunaUser1.Trim() };
            }
            if (string.IsNullOrEmpty(HuoyunyunaUser2))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.HuoyunyunaUser2 = new UserEntity { F_Id = HuoyunyunaUser2.Trim() };
            }
            if (string.IsNullOrEmpty(daokouyuanUser1))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.daokouyuanUser1 = new UserEntity { F_Id = daokouyuanUser1.Trim() };
            }
            if (string.IsNullOrEmpty(daokouyuanUser2))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.daokouyuanUser2 = new UserEntity { F_Id = daokouyuanUser2.Trim() };
            }
            if (string.IsNullOrEmpty(BanCiUser))
            {
                returncode = -1;
            }
            else
            {
                newpositionrecord.BanciUser = BanCiUser;
            }

            if (returncode != 0)
            {
                returmsg ret = new returmsg {code = returncode, message = "缺少必要参数"};
                return Content(ret.ToJson());
            }
            else
            {
                if (daokouyuanUser2.Trim() == daokouyuanUser1.Trim())
                {
                    returmsg ret = new returmsg { code = -1, message = "每人只能有1个道口员职位" };
                    return Content(ret.ToJson());
                }
                if (HuoyunyunaUser2.Trim() == HuoyunyunaUser1.Trim())
                {
                    returmsg ret = new returmsg { code = -1, message = "每人只能有1个货运员职位" };
                    return Content(ret.ToJson());
                }

                returncode = positoonapp.submitAddpositionRecord(ref newpositionrecord,ref resultmsg);
            }
            if (returncode >= 0)
            {
                //var newpaibanren = positoonapp.GetForm(f => f.State == true);
                //if (newpaibanren != null)
                //{
                //    OperatorProvider.Provider.addPositionrecord(newpaibanren.ToJson());
                //}
                //else
                //{
                //    OperatorProvider.Provider.addPositionrecord("");
                //}
                returmsg ret = new returmsg { code = returncode, message = "排班完成" };
                return Content(ret.ToJson());
            }
            else
            {
                returmsg ret = new returmsg { code = returncode, message = "排班失败，请稍后重试" };
                return Content(ret.ToJson());
            }
        }

        public ActionResult GetPosition(string keyValue)
        {
            var thispos = positoonapp.GetForm(f => f.State == true);
            return Content(thispos.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = userApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = userApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue)
        {
          int code=  userApp.SubmitForm(userEntity, userLogOnEntity, keyValue);
            if (code >= 0)
            {
                return Success("操作成功。");
            }
            else
            {
                return Error("非超级管理员无法进行此操作");
            }
        }
        [HttpPost]
        [HandlerAuthorize]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            var res = userApp.GetForm(keyValue);
            res.F_DeleteMark = true;
            //userApp.DeleteForm(keyValue);
            res.Modify(keyValue);
            userApp.UpdateForm(res);
            return Success("删除成功。");
        }
        [HttpGet]
        public ActionResult RevisePassword()
        {
            return View();
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitRevisePassword(string userPassword, string keyValue)
        {
            userLogOnApp.RevisePassword(userPassword, keyValue);
            return Success("重置密码成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DisabledAccount(string keyValue)
        {
            //UserEntity userEntity = new UserEntity();
            
            var userEntity = userApp.GetForm(keyValue);
            userEntity.F_EnabledMark = false;
            userApp.UpdateForm(userEntity);
            return Success("账户禁用成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult EnabledAccount(string keyValue)
        {
            //UserEntity userEntity = new UserEntity();
            var userEntity = userApp.GetForm(keyValue);
            //userEntity.F_Id = keyValue;
            userEntity.F_EnabledMark = true;
            userApp.UpdateForm(userEntity);
            return Success("账户启用成功。");
        }

        [HttpGet]
        public ActionResult Info()
        {
            return View();
        }
    }

    public class returmsg
    {
        public int code { get; set; }
        public string message { get; set; }
    }
}
